package src.leetcode.greedy.medium_0763;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class partitionLabels {
    public static void main(String[] args) {
        Solution solution = new Solution();
        System.out.println(solution.partitionLabels("ababcbacadefegdehijhklij"));
    }
}


class Solution {
    public List<Integer> partitionLabels(String s) {
        List<Integer> ans = new ArrayList<>();
        int left = 0;
        while (left < s.length()){
            int last = 0;
            for(int j = left; j < s.length(); j++){
                if(s.charAt(j) == s.charAt(left)){
                    last = j;
                }
            }

            int right;
            Set<Character> set = new HashSet<>();
            do {
                right = last;
                for(int j = left; j <= right; j++){
                    set.add(s.charAt(j));
                }

                for(int j = right + 1; j < s.length(); j++) {
                    if(set.contains(s.charAt(j))) last = j;
                }

            }while (right != last);


            ans.add(right - left + 1);
            left = right + 1;
        }

        return ans;
    }
}

